package com.itheima.jobs;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisConstant;
import com.itheima.pojo.Member;
import com.itheima.pojo.OrderSettingList;
import com.itheima.service.MemberService;
import com.itheima.service.OrderSettingListService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.EmailUtils;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 定时发送邮件提醒客户
 *
 * @author W.J.Y
 */
public class sendOrder {

    @Reference(version = "1.0.0")
    private OrderSettingListService orderSettingListService;
    @Reference(version = "1.0.0")
    private MemberService memberService;

    public void sendMail() throws Exception {
        System.out.println("邮件发送任务执行了！");
        //将今天的日期格式化yyyy-MM-dd
        String dateStr = DateUtils.parseDate2String(new Date());
        //获取预约列表所有数据
        System.out.println("我获取了预约列表所有数据！");
        List<OrderSettingList> all = orderSettingListService.findAll();
        if (all!=null){
            //存在预约列表数据，遍历
            for (OrderSettingList orderSettingList : all) {
                //获取该预约信息的预约日期
                Date date = orderSettingList.getDate();
                System.out.println("我获取了预约列表预约的日期！");
                if (date==null){
                    //预约日期为null，跳过本次循环
                    continue;
                }
                //将该预约信息减去一天
                Date date1 = new Date(date.getTime() - 1000 * 60 * 60 * 24);

                if (date1!=null&&dateStr!=null){
                    System.out.println("预约信息减去一天："+date1);
                    //预约时间与今天不为空
                    String dateStr1 = DateUtils.parseDate2String(date1);
                    System.out.println("预约信息减去一天之后的："+dateStr);
                    //判断预约时间的前一天与今天一致
                    if (dateStr.equals(dateStr1)){
                        //根据会员档案号获取会员对象

                        Member member = (Member)memberService.findByFileId(orderSettingList.getFileNumber()).getData();
                        if (member==null){
                            return;
                        }
                        System.out.println("获取了会员信息："+member.getEmail());
                        //根据邮箱信息发送邮件
                        //EmailUtils.sendMail(member.getEmail(),"尊敬的客户，您好！您预约的体检将在明天进行，请留意时间。","");
                    }
                }
            }
        }
    }
}
